<template>
  <div>
    <el-dialog :title="title" :closeOnClickModal="false" :visible.sync="show" width="650px">
      <el-form label-position="right" :model="selectTable" ref="addForm" label-width="80px" :inline="false" autocomplete="off">
        <el-row>
          <el-col :span="14">
            <el-form-item label="名称" prop="name" :rules="[rule[0]]">
              <el-input placeholder="请输入" v-model="selectTable.name" maxLength="100"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="标签名" prop="labelName" :rules="[rule[0]]">
              <el-input placeholder="请输入" v-model="selectTable.labelName" maxLength="100"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="状态" prop="isEnable" :rules="[rule[0]]">
              <el-switch v-model="selectTable.isEnable" :active-value="0" :inactive-value="1" active-text="启用" inactive-text="停用">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="备注" prop="comment">
              <el-input placeholder="请输入" v-model="selectTable.comment" maxLength="50"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            属性名
          </el-col>
          <el-col :span="16">
            允许值
          </el-col>
          <template v-for="(item,index) in selectTable.extlist">
          <el-col :span="8">
            <el-form-item label="" label-width="0" :prop="`extlist[${index}].name`" :rules="[rule[0]]" style="margin-bottom:0">
              <el-input size="mini" placeholder="请输入" v-model="item.name" maxLength="20"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="" label-width="0" :prop="`extlist[${index}].allowValue`" :rules="[rule[0]]" style="margin-bottom:0">
              <el-input size="mini" placeholder="请输入" v-model="item.allowValue" maxLength="100"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="" label-width="0" style="margin-bottom:0">
              <el-button size="mini" @click="handleRemove(index)" icon="el-icon-minus" style="padding:7px 8px;"></el-button>
            </el-form-item>
          </el-col>
          </template>
        </el-row>
        <el-row :gutter="10" style="background:#f4f4f4;">
          <el-col :span="22">
            <div style="visibility:hidden;">+</div>
          </el-col>
          <el-col :span="2">
            <el-form-item label="" label-width="0" style="margin-bottom:0">
              <el-button size="mini" type="success" @click="handleAdd" icon="el-icon-plus" style="padding:7px 8px;"></el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '@/utils/api'
import singleImage from '@/components/Upload/singleImage'
export default {
  components: {
    editor: () => import('@/components/Ueditor'),
    singleImage
  },
  props: {
    data: Object
  },
  created() {
    this.selectTable = this.JSONs(this.data)
  },
  watch: {
    show(newVal) {
      this.$parent.edit = false
    }
  },
  computed: {
    title() {
      if (this.selectTable.method === 'edit') {
        return '修改'
      } else if (this.selectTable.method === 'add') {
        return '添加'
      }
    }
  },
  data() {
    return {
      pload: false,
      rule: [
        { required: true, message: '必填项', trigger: 'change' }
      ],
      show: true,
      selectTable: {}
    }
  },
  methods: {
    handleAdd() {
      this.selectTable.extlist.push({
        name: '',
        allowValue: ''
      })
    },
    handleRemove(index) {
      this.selectTable.extlist.splice(index, 1)
    },
    async submitForm(formName) {
      await this.$refs[formName].validate()
      try {
        this.pload = true
        if (this.selectTable.method === 'edit') {
          await api.tplLabelModify(this.selectTable)
        } else if (this.selectTable.method === 'add') {
          await api.tplLabelSave(this.selectTable)
        }
        this.$notify({ title: '操作成功', type: 'success', duration: 5000 })
        this.$parent.edit = false
        this.$emit('getData')
      } catch (e) {
        this.pload = false
      }
    }
  }
}
</script>